package com.ganzi.manager.activiti.util;

import org.activiti.bpmn.model.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import static com.ganzi.manager.activiti.util.ActivitiUtil.*;

/**
 * @author lxd
 * @create 2019/6/28
 * 自定义流程节点
 */
@Service
public class ProcessUtil {

    /**
     * 创建任务节点
     */
    public static UserTask createUserTask(String id, String name, Object assignee,List<String> groups) {
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        if(assignee instanceof List){
            userTask.setCandidateUsers((List<String>) assignee);
        }else{
            userTask.setAssignee((String)assignee);
        }
        if(groups != null && groups.size()>0)userTask.setCandidateGroups(groups);
        return userTask;
    }

    /**
     * 创建排他网关节点
     */
    public static ExclusiveGateway createExclusiveGateway(String id, String name) {
        ExclusiveGateway gateway = new ExclusiveGateway();
        gateway.setName(name);
        gateway.setId(id);
        return gateway;
    }
    /**
     * 连线
     * @param from
     * @param to
     * @return
     */
    public static SequenceFlow createSequenceFlow(String from, String to) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        return flow;
    }
    public static SequenceFlow createSequenceFlow(String from, String to,String condition) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        //连线添加条件
        flow.setConditionExpression(condition);
        return flow;
    }

    /**
     * 开始节点
     * @return
     */
    public static StartEvent createStartEvent(String formKey) {
        StartEvent startEvent = new StartEvent();
        startEvent.setId(NODE_IDPrefix+START_NODE_ID);
        startEvent.setName("开始");
        if(StringUtils.isNotBlank(formKey))
        startEvent.setFormKey(formKey);
        return startEvent;
    }

    /**
     * 结束节点
     * @return
     */
    public static EndEvent createEndEvent() {
        EndEvent endEvent = new EndEvent();
        endEvent.setId(NODE_IDPrefix+END_NODE_ID);
        endEvent.setName("结束");
        return endEvent;
    }
}
